package com.genntii.examManager.controller;

import com.genntii.examManager.common.constant.CommonConstant;
import com.genntii.examManager.common.result.PageResult;
import com.genntii.examManager.common.result.Result;
import com.genntii.examManager.domain.dto.ExamAddDTO;
import com.genntii.examManager.domain.dto.VocabulariesDTO;
import com.genntii.examManager.domain.question.dto.VocabularyDTO;
import com.genntii.examManager.domain.question.mongo.QuestionBankMongo;
import com.genntii.examManager.domain.question.mongo.Vocabularies;
import com.genntii.examManager.domain.vo.Exam4tVO;
import com.genntii.examManager.domain.vo.ExamComplete;
import com.genntii.examManager.domain.vo.ExamRecordVO;
import com.genntii.examManager.domain.vo.QuestionBankMongoVO;
import com.genntii.examManager.service.ExamRecordService;
import com.genntii.examManager.service.ExamService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/exam/t")
public class Exam4tController {

    @Resource
    private ExamService examService;

    @Resource
    private ExamRecordService examRecordService;

    @PutMapping("/status")
    public Result updateStatus(@RequestParam("status")Byte status,
                               @RequestParam("examId")Long examId){
        examService.updateStatus(examId,status);
        return Result.success();
    }

    @PostMapping
    public Result addExam(@RequestBody ExamAddDTO examAddDTO,
                          @RequestParam(required = false) ArrayList<Long> mongoId,
                          HttpServletRequest request){
        Long userId = Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.TEACHER_ID))) ;
        examService.addExam(examAddDTO,mongoId,userId);
        return Result.success();
    }


    @GetMapping("/questionBank")
    public Result<PageResult<QuestionBankMongoVO>> getQuestionBankList(@RequestParam("pageNum")Integer pageNum,
                                                                       @RequestParam("pageSize")Integer pageSize,
                                                                       @RequestParam("subjectId")Long subjectId,
                                                                       @RequestParam(value = "questionType", required = false) Byte questionType,
                                                                       @RequestParam(value = "keyword", required = false) String keyword){
        // questionType: 1-单选 2-多选 3-判断 4-简答
        return Result.success(examService.getQuestionBankList(pageNum, pageSize, subjectId,questionType,keyword));
    }

    @PutMapping("finalExam")
    public Result setFinalExam(@RequestParam("examId") Long examId,
                               @RequestParam("courseId") Long courseId,
                               HttpServletRequest request){
        Long userId = Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.TEACHER_ID))) ;
        examService.setFinalExam(examId,courseId,userId);
        return Result.success();
    }

    @GetMapping("/vocabulary/{examId}")
    public Result<PageResult<Vocabularies>> getVocabulary(@PathVariable("examId") Long examId,
                                              @RequestParam("pageNum") Integer pageNum,
                                              @RequestParam("pageSize") Integer pageSize){

        return Result.success(examRecordService.getVocabulary(pageNum, pageSize, examId));
    }


    @PostMapping("/vocabulary/{examId}")
    public Result grading(@PathVariable("examId") Long examId,
                        @RequestBody ArrayList<VocabulariesDTO> vocabulariesDTO){
        examRecordService.grading(examId, vocabulariesDTO);
        return Result.success();
    }

    @GetMapping("/stuStatus")
    public Result<PageResult<ExamComplete>> getStudentExamStatus(@RequestParam Long examId,
                                                                @RequestParam Integer pageNum,
                                                                @RequestParam Integer pageSize){
        return Result.success(examRecordService.getStudentExamCompleteStatus(pageNum, pageSize, examId));
    }

    @GetMapping
    public Result<PageResult<Exam4tVO>> getExamListVO(@RequestParam Integer pageNum,
                                                      @RequestParam Integer pageSize,
                                                      @RequestParam Long courseId){
        return Result.success(examRecordService.getExamListVO4t(pageNum,pageSize,courseId));
    }
}
